package com.xianyun.site.auth.common.config.bean;

import com.xianyun.site.auth.provider.IAuthenticationProvider;
import com.xianyun.site.auth.service.IUserDetailsService;
import com.xianyun.site.auth.service.filter.IAuthenticationFilter;
import com.xkcoding.justauth.AuthRequestFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.security.web.authentication.AuthenticationFailureHandler;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

/**
 * 扩展第三方登录配置
 *
 * @author songyinyin
 * @date 2020/5/4 下午 07:58
 */
@Configuration
public class IAuthenticationSecurityConfig extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {

    @Autowired
    private IUserDetailsService iUserDetailsService;

    @Autowired
    private AuthRequestFactory authRequestFactory;
    @Autowired
    private AuthenticationSuccessHandler successHandler;
    @Autowired
    private AuthenticationFailureHandler failureHandler;


    @Override
    public void configure(HttpSecurity builder)  {
        // 1. 初始化 ExtendAuthenticationFilter
        IAuthenticationFilter filter = new IAuthenticationFilter();
        filter.setAuthenticationManager(builder.getSharedObject(AuthenticationManager.class));
        filter.setAuthenticationSuccessHandler(successHandler);
        filter.setAuthenticationFailureHandler(failureHandler);
        filter.setAuthRequestFactory(authRequestFactory);

        // 2. 初始化 ExtendAuthenticationProvider
        IAuthenticationProvider provider = new IAuthenticationProvider();
        provider.setExtendUserDetailsService(iUserDetailsService);

        // 3. 将设置完毕的 Filter 与 Provider 添加到配置中，将自定义的 Filter 加到 UsernamePasswordAuthenticationFilter 之前
        builder.authenticationProvider(provider).addFilterAt(filter,UsernamePasswordAuthenticationFilter.class);
        builder.authenticationProvider(provider).addFilterBefore(filter, UsernamePasswordAuthenticationFilter.class);
    }
}
